package com.lzl.controller;

import com.ktjiaoyu.entity.SysUser;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import java.util.Map;

@Controller
@RequestMapping("/dev")
public class HelloController {
    private static final Logger LOGGER = LoggerFactory.getLogger(HelloController.class);

    @GetMapping("/login1")
    public ModelAndView login1(){
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    @PostMapping("/login")
    public String login(String username,String password,Model model){
        model.addAttribute("username",username);
        return "index";
    }


    @RequestMapping("/hello")
    protected ModelAndView hello(@RequestParam(name = "realName",required = false) String name) throws Exception {
        LOGGER.info("你好{}，欢迎学习SpringMVC！",name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("realName",name);
        modelAndView.setViewName("hello");
        return modelAndView;
    }
//    @RequestMapping("/hello")
//    protected String hello(@RequestParam(name = "realName",required = false) String name) throws Exception{
//        LOGGER.info("你好{}，欢迎学习SpringMVC！",name);
//        return "hello";
//    }

    @RequestMapping("/welcome")
    protected String welcome(HttpServletRequest request) throws Exception{
        LOGGER.info("进入控制器welcome方法，请求路径为：{}",request.getRequestURI());
        return "hello";
    }

    @RequestMapping("model")
    public String model(Model model,@RequestParam(name = "realName",required = false) String name) throws Exception{
        LOGGER.info("你好{}，在参数注入Model对象也可以出参！",name);
        model.addAttribute("realName",name);
        SysUser sysUser = new SysUser();
        sysUser.setRealName(name);
        model.addAttribute("sysUser",sysUser);
        return "hello";
    }

    @RequestMapping("/map")
    public String model(Map map,@RequestParam(name = "realName",required = false) String name) throws Exception{
        LOGGER.info("你好{}，在参数注入Map对象也可以出参！",name);
        map.put("realName",name);
        SysUser sysUser = new SysUser();
        sysUser.setRealName(name);
        map.put("sysUser",sysUser);
        return "hello";
    }
}
